<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 撞单
 *
 * @icon fa fa-circle-o
 */
class Collision extends Backend
{

    /**
     * Collision模型对象
     * @var \app\common\model\Collision
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Collision;
        $this->view->assign("statusList", $this->model->getStatusList());
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model

                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','connection_id','connection_name','connection_mobile','connection_mobile_see','connection_mobile_two',
                    'connection_mobile_two_see','bind_staff_name','bind_staff_mobile','staff_name','staff_mobile','staff_connection_id','status',
                    'handle_time','createtime','canal_name','remark','bind_staff_ibec_name','staff_ibec_name']);

            }
            $untreatedTotal=$this->model
                ->where([
                    "status"=>'0'
                ])
                ->count();
            $result = array("total" => $list->total(), "rows" => $list->items(),"untreatedTotal"=>$untreatedTotal);

            return json($result);
        }
        return $this->view->fetch();
    }



    /**
     * 改为已处理
     * */
    public function ok()
    {
        if (!$this->request->param("ids")) $this->error('id为空');
        $param=$this->request->param();
        $row = $this->model->get($param["ids"]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $param["handle_time"]=time();
        $result = $row->allowField(true)->save($param);
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were updated'));
        }
    }

    /**
     * 查看详情
     * */
    public function see($id)
    {
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->assign("row",$row);
        return $this->view->fetch();
    }

    /**
     * 彩色角标
     * */
    public function colorbadge(){
        $total =$this->model
            ->where([
                "status"=>'0'
            ])
            ->count();
        $result = array("total" => $total);
        return json($result);
    }

}
